package com.company.cloud.company.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.IdStrParam;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.company.constant.CompanyConstant;
import com.company.cloud.company.modules.api.request.CompanyAddRequest;
import com.company.cloud.company.modules.api.request.CompanyQueryRequest;
import com.company.cloud.company.modules.api.request.CompanyUpdateRequest;
import com.company.cloud.company.modules.api.response.CompanyFull;
import com.company.cloud.company.modules.app.controller.req.CompanyQuery4PageParam;
import com.company.cloud.company.modules.cache.service.CompanyCacheService;
import com.company.cloud.company.modules.mgr.service.CompanyMgrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
@RequestMapping("/app/user/company")
@Api(tags="Company_App_公司服务22")
public class CompanyController {

    @Autowired
    private CompanyMgrService companyMgrService;
    @Autowired
    private CompanyCacheService companyCacheService;


    @PostMapping("/query")
    @ApiOperation(value="公司查询", response= CompanyFull.class)
    public BaseResult query(@RequestBody CompanyQuery4PageParam params) {
        CompanyQueryRequest request=new CompanyQueryRequest();
        request.setCompanyNo(params.getCompanyNo());
        request.setName(params.getName());
        request.setType(params.getType());
        request.setSourceType(params.getSourceType());
        request.setSource(params.getSource());
        request.setPage(params.getPage());
        request.setRows(params.getRows());
        request.setState(CompanyConstant.CompanyState.CS_Open.getCode());
        IPage<CompanyFull> data= companyMgrService.query(request);
        return BaseResult.ok(data);
    }



//    @Login
//    @PostMapping("/add")
//    @ApiOperation(value="公司新增", response= BaseResult.class)
//    public BaseResult add(@RequestBody CompanyAddRequest params) {
//        ValidatorUtils.validate(params);
//        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
//        companyMgrService.save(params,userInfo);
//        return BaseResult.ok();
//    }
//


    @Login
    @PostMapping("/edit")
    @ApiOperation(value="公司修改", response= BaseResult.class)
    public BaseResult edit(@RequestBody CompanyUpdateRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setCompanyNo(userInfo.getCompanyNo());
        companyMgrService.update(params,userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/info")
    @ApiOperation(value="公司获取", response= CompanyFull.class)
    public BaseResult info(@RequestBody IdStrParam params) {
        ValidatorUtils.validate(params);
        CompanyFull data= companyCacheService.getCompanyByDB(params.getId());
        return BaseResult.ok(data);
    }









}
